<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\validate\Column as validateColumn ;
use app\admin\model\Column as modelColumn;
use think\facade\Hook;

class Column extends Base{
    public function __construct(){
        parent::__construct();
    }

    /**
     * @param array $where
     * @param int $tree
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($where=[],$tree=0){
        $where = $this->_where($where);
        $column = new modelColumn;
        if($tree){
            $_column = $column::field('id,fid,title,name as `index`,icon')
                ->where($where)->where('status','eq',0)
                ->select();
            $_column = $_column->toArray();
            $_column = \service\Category::unlimitedForLevel($_column);
            $_column = json_decode(json_encode($_column,true),true);
            $_column = array_remove_empty($_column);
        }else{
            $_column =$column::all($where);
//            $_column = \service\Category::limitForLevel($_column);
        }
        return $this->__s('',$_column);
    }

    /**
     * @param int $id
     * @return array|\think\Response
     */
    public function save($id=0){
        $data = request()->post();
        $validate = new validateColumn;
        if(!$validate->check($data)){
            return $this->__e($validate->getError());
        }
        $column = new modelColumn;
        if($id){
            if(!$column->allowField(true)->isUpdate(true)->save($data)){
                return $this->__e('更新失败');
            }
            $sql = $column->getLastSql();
            $params = [
                'aid'=>self::$uid,
                'action'=>'更新'."栏目，编号：{$id}",
                'sql'=>$sql
            ];
            Hook::listen('admin_log',$params);
            return $this->__s('更新成功');
        }else{
            if(!$id = $column->save($data)){
               return $this->__e('添加失败');
            }
            $sql = $column->getLastSql();
            $params = [
                'aid'=>self::$uid,
                'action'=>'新增'."栏目，编号：{$id}",
                'sql'=>$sql
            ];
            Hook::listen('admin_log',$params);
            return $this->__s('添加成功');
        }
    }

    /**
     * @param int $id
     * @return array|\think\Response
     */
    public function delete($id=0){
        if(!$id){
            return $this->__e('缺少id');
        }
        $info = (new modelColumn())->find($id);
        if(!$info){
            return $this->__e('栏目已删除');
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>'删除'."栏目，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('删除成功');
    }

    /**
     * 读取
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        if(!$id){
            return $this->__e('缺少id');
        }
        $column = new modelColumn;
        $_column = $column::get($id);
        if(!$_column){
            return $this->__e('没有数据');
        }
        return $this->__s('',$_column);
    }

    /**
     * @param int $id
     * @param int $status
     * @return \think\Response
     */
    public function update($id=0,$status=0){
        if(!$id){
           return $this->__e('缺少id');
        }
        $column = new modelColumn;
        $_column = $column::get($id);
        if(!$_column){
            return $this->__e('没有数据');
        }
        $_column->status=$status;
        if(!$_column->save()){
            return $this->__e('修改失败');
        }
        $sql = $_column->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>'删除'."栏目，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('修改成功');
    }

    /**
     * @param int $name
     * @route('admin/column/one','get')
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function column_one($name=0){
        $column = new modelColumn;
        if(!$name){
            return $this->__e('缺少参数');
        }
        $_m = $column
            ->field('id,fid,title,name,ico')
            ->where('name','eq',$name)->find();
        if(!$_m){
           return $this->__e('无数据');
        }
        return $this->__s('',$_m);
    }
}